package com.itheima;

import org.junit.jupiter.api.Test;

import javax.annotation.processing.SupportedAnnotationTypes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {
    public static void main(String[] args) {
        //创建一个集合，存储多个字符串元素
        List<String> names = new ArrayList<>();
        names.add("张三");
        names.add("张三丰");
        names.add("张无2");
        names.add("李四");
        names.add("王五");
        names.add("赵六");
        List<String> list1 = new ArrayList<>();
        names.forEach(name ->{
            if (name.startsWith("张")) {
                list1.add(name);
            }
        });

        List<String> list2 = new ArrayList<>();
        names.forEach(name ->{
            if (name.length() == 3){
                list2.add(name);
            }
        });
        list2.forEach(System.out::println);
        System.out.println("--------------------");


        names.stream()
                .filter(s -> s.startsWith("张"))
                .filter(s -> s.length() == 3)
                .forEach(System.out::println);
    }
    @Test
    public void test1(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);
        // List<Integer> list1 = list.stream().filter(integer -> integer % 2 == 0).collect(Collectors.toList());

        List<Integer> list1 = list.stream().filter(integer -> integer % 2 == 0).toList();
        System.out.println(list1);
    }
    @Test
    public void test2(){
        List<String> list = new ArrayList<>();
        list.add("张三,23");
        list.add("李四,24");
        list.add("王五,25");
        Map<Object,Object> map=list.stream().filter(s -> {
            String[] split = s.split(",");
            int age= Integer.parseInt(split[1]);
            return age > 23;
        }).collect(Collectors.toMap(s -> {
            String[] split = s.split(",");
            return split[0];
        },  s -> {
            String[] split = s.split(",");
            return split[1];
        }));
        map.forEach((k,v) -> {
            System.out.println(k + ".." + v);
        });
    }
    @Test
    public void test3(){
        List<String> list1 = new ArrayList<>();
        list1.add("佟大为");
        list1.add("黄渤");
        list1.add("张学友");
        List<String> list2 = new ArrayList<>();
        list2.add("高圆圆");
        list2.add("迪丽热巴");
        list2.add("杨幂");
        list2.add("杨紫");
        Stream<String> stream1 =list1.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.length()==3;
            }
        }).limit(2);
        Stream<String> stream2 =list2.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("杨");
            }
        }).skip(1);
        Stream.concat(stream1,stream2).forEach(System.out::println);
    }
}
